﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;
using CSP.Bll;
using CSP.Bll.IObjectBlls;
using CSP.Model.CSP;
using CSP.Model.CSPEnum;
using CSP.Common.Tools;

namespace CSP.Web.UserControls
{
    public partial class RateList : CSPUserControlBase
    {
        private const string SEARCHIDSKEY = "searchids";
        private const string RATEBUSINESSTYPEKEY = "RATEBUSINESSTYPE";

        private int _serviceId;
        private string _userName;



        private List<int> SearchIds
        {
            get
            {
                return ViewState[SEARCHIDSKEY] as List<int>;

            }
            set
            {
                ViewState[SEARCHIDSKEY] = value;
            }
        }

        public int ServiceId
        {
            get
            { return _serviceId; }
            set
            { _serviceId = value; }
        }

        public string UserName
        {
            get
            { return _userName; }
            set
            { _userName = value; }
        }

        public RateListType RateBusinessType
        {
            get { return (RateListType)ViewState[RATEBUSINESSTYPEKEY]; }
            set { ViewState[RATEBUSINESSTYPEKEY] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
            else
            {
                //R_Rating.DataSource = new List<CreditLine>();
                //R_Rating.DataBind();
            }
        }

        /// <summary>
        /// RateList的绑定展现
        /// </summary>
        public void RateListBind()
        {
            InitCreditList();
        }

        protected void S_RateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DropDownList S_RateType = R_Rating.Controls[0].FindControl("S_RateType") as DropDownList;
            int index = S_RateType.SelectedIndex;
            if (S_RateType.SelectedValue == "-1")
            {
                InitCreditList();
            }
            else
            {
                InitSearchId();
                int status = Convert.ToInt32(S_RateType.SelectedValue);
                SearchIds = FindIdsByIdsAndCreditRatingType(SearchIds, (CreditRatingType)status);
            }

            InitParm();
            List<CreditLine> creditLines = GetCreditLineByIds(SearchIds, U_CreditLinePager.PageIndex, U_CreditLinePager.PageSize);

            BindListUI(creditLines);
            S_RateType.SelectedIndex = index;
            S_RateType.Focus();
        }

        protected void S_RateContentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DropDownList s_RateContentType = R_Rating.Controls[0].FindControl("S_RateContentType") as DropDownList;
            int index = S_RateContentType.SelectedIndex;

            if (S_RateContentType.SelectedValue == "-1")
            {
                InitCreditList();
            }
            else
            {
                InitSearchId();
                int status = Convert.ToInt32(S_RateContentType.SelectedValue);
                SearchIds = FindIdsByIdsAndContentType(SearchIds, status);
            }

            InitParm();
            List<CreditLine> creditLines = GetCreditLineByIds(SearchIds, U_CreditLinePager.PageIndex, U_CreditLinePager.PageSize);

            BindListUI(creditLines);
            S_RateContentType.SelectedIndex = index;
            S_RateContentType.Focus();
        }

        protected void U_CreditLinePager_IndexChange(object sender, EventArgs e)
        {
            List<CreditLine> orders = GetCreditLineByIds(SearchIds, U_CreditLinePager.PageIndex, U_CreditLinePager.PageSize);
            BindListUI(orders);
        }

        private void InitCreditList()
        {
            InitSearchId();
            List<CreditLine> orders = GetCreditLineByIds(SearchIds, U_CreditLinePager.PageIndex, U_CreditLinePager.PageSize);
            BindListUI(orders);

            InitParm();
        }

        /// <summary>
        /// 初始化搜索数据
        /// </summary>
        private void InitSearchId()
        {

            if (RateBusinessType == RateListType.FromConsume)
            {
                SearchIds = FindIdsFromConsumeByServiceUser();
            }
            else if (RateBusinessType == RateListType.FromService)
            {
                SearchIds = FindIdsFromServiceByConsumeUser();
            }
            else if (RateBusinessType == RateListType.RateOther)
            {
                SearchIds = FindIdsByFromUser();
            }
            else if (RateBusinessType == RateListType.RateOther)
            {
                SearchIds = FindIdsFromConsumeByServiceId();
            }
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        private void InitParm()
        {
            S_RateType.SelectedIndex = 0;
            S_RateContentType.SelectedIndex = 0;
            InitPager();
        }

        /// <summary>
        /// 初始化分页控件
        /// </summary>
        private void InitPager()
        {
            SetPagerParm(0, 15, SearchIds.Count);
        }

        /// <summary>
        /// 设置分页参数
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        private void SetPagerParm(int pageIndex, int pageSize, int pageCount)
        {
            this.U_CreditLinePager.PageSize = pageSize;
            this.U_CreditLinePager.PageIndex = pageIndex;
            this.U_CreditLinePager.ItemCount = pageCount;
        }

        /// <summary>
        /// 绑定控件
        /// </summary>
        /// <param name="creditLines"></param>
        private void BindListUI(List<CreditLine> creditLines)
        {
            R_Rating.DataSource = creditLines;
            R_Rating.DataBind();
        }

        /// <summary>
        /// 来自消费者的评价
        /// </summary>
        /// <returns></returns>
        private List<int> FindIdsFromConsumeByServiceUser()
        {
            List<int> result = new List<int>();
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            using (ICreditLineBll creditLineBll = bllFactory.GetCreditLineBll())
            {
                result = creditLineBll.FindIdsFromConsumeByServiceUser(_userName);
            }

            return result;
        }

        /// <summary>
        /// 来自服务者的评价
        /// </summary>
        /// <returns></returns>
        private List<int> FindIdsFromServiceByConsumeUser()
        {
            List<int> result = new List<int>();
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            using (ICreditLineBll creditLineBll = bllFactory.GetCreditLineBll())
            {
                result = creditLineBll.FindIdsFromServiceByConsumeUser(_userName);
            }

            return result;
        }

        /// <summary>
        /// 给他人的评价
        /// </summary>
        /// <returns></returns>
        private List<int> FindIdsByFromUser()
        {
            List<int> result = new List<int>();
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            using (ICreditLineBll creditLineBll = bllFactory.GetCreditLineBll())
            {
                result = creditLineBll.FindIdsByFromUser(_userName);
            }

            return result;
        }

        private List<int> FindIdsFromConsumeByServiceId()
        {
            List<int> result = new List<int>();
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            using (ICreditLineBll creditLineBll = bllFactory.GetCreditLineBll())
            {
                result = creditLineBll.FindIdsFromConsumeByServiceId(_serviceId);
            }

            return result;
        }

        /// <summary>
        /// 根据评价Ids 获取来评论不等于空的Ids
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="contentType">0 无内容 ，1有内容</param>
        /// <returns></returns>
        private List<int> FindIdsByIdsAndContentType(List<int> ids, int contentType)
        {
            List<int> result = new List<int>();
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            using (ICreditLineBll creditLineBll = bllFactory.GetCreditLineBll())
            {
                result = creditLineBll.FindIdsByIdsAndContentType(ids, contentType);
            }

            return result;
        }

        /// <summary>
        /// 根据评价Ids 获取来评论不等于空的Ids
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        private List<int> FindIdsByIdsAndCreditRatingType(List<int> ids, CreditRatingType creditRatingType)
        {
            List<int> result = new List<int>();
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            using (ICreditLineBll creditLineBll = bllFactory.GetCreditLineBll())
            {
                result = creditLineBll.FindIdsByIdsAndCreditRatingType(ids, creditRatingType);
            }

            return result;
        }


        private List<CreditLine> GetCreditLineByIds(List<int> ids, int pageIndex, int pageSize)
        {
            List<CreditLine> result = new List<CreditLine>();
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            using (ICreditLineBll creditLineBll = bllFactory.GetCreditLineBll())
            {
                var creditLines = creditLineBll.GetCreditLineByIds(ids, pageIndex, pageSize);
                if (creditLines.Count != 0)
                {
                    result = creditLines.ToList();
                }
            }

            return result;
        }

        #region UI显示名称
        protected string GetStyleByCreditRating(int creditRating)
        {
            if ((CreditRatingType)creditRating == CreditRatingType.Good)
            {
                return "rate-ok";
            }
            else if ((CreditRatingType)creditRating == CreditRatingType.Bad)
            {
                return "rate-normal";
            }
            else
            {
                return "rate-bad";
            }
        }

        protected string GetCnNameCreditRating(int creditRating)
        {
            if ((CreditRatingType)creditRating == CreditRatingType.Good)
            {
                return "好评";
            }
            else if ((CreditRatingType)creditRating == CreditRatingType.Bad)
            {
                return "中评";
            }
            else
            {
                return "差评";
            }
        }

        protected string GetCreditImgNameByUserName(string fromUserName, string toUserName)
        {
            string userName = "";
            if (RateBusinessType == RateListType.FromConsume)
            {
                userName = fromUserName;
            }
            else if (RateBusinessType == RateListType.FromService)
            {
                userName = fromUserName;
            }
            else if (RateBusinessType == RateListType.RateOther)
            {
                userName = toUserName;
            }
            else if (RateBusinessType == RateListType.ServiceInfoRate)
            {
                userName = fromUserName;
            }
            int code = GetUserCredit(userName);
            return GetConsumptionCreditGrade(code);
        }

        /// <summary>
        /// 获取用户信誉度对象
        /// </summary>
        /// <returns></returns>
        private int GetUserCredit(string userName)
        {

            UserCredit result = null;
            using (var userCreditBll = CSPBllFactory.GetInstance().GetUserCreditBll())
            {
                result = userCreditBll.GetByUser(userName);
            }



            return result.ServiceCredit;
        }

        /// <summary>
        /// 获取消费等级
        /// </summary>
        /// <param name="score"></param>
        /// <returns></returns>
        private string GetConsumptionCreditGrade(int score)
        {
            return CommonTool.GetConsumptionCreditGradeByScore(score);
        }

        /// <summary>
        /// 等级服务等级
        /// </summary>
        /// <param name="score"></param>
        /// <returns></returns>
        private string GetServiceCreditGrade(int score)
        {
            return CommonTool.GetServiceCreditGradeByScore(score);
        }

        #endregion

        public enum RateListType
        {
            /// <summary>
            /// 来自消费者的评价
            /// </summary>
            FromConsume = 0,

            /// <summary>
            /// 来自服务者的评价
            /// </summary>
            FromService = 1,

            /// <summary>
            /// 对他人的评价
            /// </summary>
            RateOther = 2,

            /// <summary>
            /// 基于服务信息的评价
            /// </summary>
            ServiceInfoRate = 3
        }
    }
}